package com.yymx.toback.common.sms;



import com.yymx.toback.common.redis.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Optional;
import java.util.Random;


@Component
public class SmsFilterUtil {
    private Logger logger = LoggerFactory.getLogger(SmsFilterUtil.class);
    @Value("${sms.send.count}")
    private Integer maxSendCount;
    @Value("${sms.send.sendInterval}")
    private Integer sendInterval;
    @Value("${sms.send.ipMaxSendCount}")
    private Integer ipMaxSendCount;
    @Resource
    RedisUtil redisUtil;
    /**
     * 发送次数一天不能超过15次
     *
     * @param mobile
     * @return
     */
    public boolean mobileFilter(String mobile) {

        String count = (String) Optional.ofNullable(redisUtil.get("SMS:send-count_" + mobile)).orElse("0");
        logger.info("当前号码[{}]发送次数[{}]", mobile, count);
        if (Integer.valueOf(count) <= maxSendCount) {
            return true;
        }
        logger.info("当前号码" + mobile + "当日发送已超过15条");
        return false;
    }

    /**
     * 重复发送时间最低间隔一分钟
     *
     * @param mobile
     * @return
     */
    public boolean canSendInMinutes(String mobile) {
        Optional key = Optional.ofNullable(redisUtil.get("SMS:sendTime_" + mobile));
        if (key.isPresent()) {
            logger.info("当前号码[{}]发送时间未超过{[]}分钟",mobile,sendInterval);
            return false;
        }

        return true;
    }

    /**
     * 对IP进行过滤
     *
     * @param ip
     * @return
     */
    public boolean ipFilter(String ip) {
        if ("127.0.0.1".equals(ip)) {
            return true;
        }
        String count = (String) Optional.ofNullable(redisUtil.get("SMS:allow-ip_" + ip)).orElse("0");
        if (Integer.valueOf(count) >= ipMaxSendCount) {
            logger.info("当前IP发送已超过最大限制{[]}",ip);
            return false;
        }
        return true;
    }

    /**
     * 创建六位的验证码
     *
     * @return
     */
    public static synchronized String createIndentifyCode() {
        String RADOM_NUM = "0123456789";
        StringBuffer sb = new StringBuffer(6);
        for (int i = 0; i < 6; i++) {
            char ch = RADOM_NUM.charAt(new Random().nextInt(RADOM_NUM.length()));
            sb.append(ch);
        }
        return sb.toString();
    }

    /**
     * 调用完成后记录一下调用过的信息
     */
    public void saveSmsLog(String mobile, String ip) {
        Calendar calendar = Calendar.getInstance();
        Long now = calendar.getTimeInMillis();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Long betweenTime = (calendar.getTimeInMillis() - now) / 1000;
        Object count = Optional.ofNullable(redisUtil.get("SMS:send-count_" + mobile)).orElse(0);
        Object ipRequestCount = Optional.ofNullable(redisUtil.get("SMS:allow-ip_" + ip)).orElse(0);
        redisUtil.set("SMS:send-count_" + mobile, Integer.parseInt(count + "") + 1, betweenTime);
        redisUtil.set("SMS:allow-ip_" + ip, Integer.parseInt(ipRequestCount + "") + 1, betweenTime);
        redisUtil.set("SMS:sendTime_" + mobile, Integer.parseInt(count + "") + 1, sendInterval);

    }
}
